//
//  ViewController.m
//  JeckeMasonry
//
//  Created by 周都 on 16/3/29.
//  Copyright © 2016年 周都. All rights reserved.
//

#import "ViewController.h"
#import "JackeZController.h"
#import "Base1View.h"
#import "TableViewCell.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *TableView;

@property (strong, nonatomic) NSArray *array;

@property (nonatomic,strong) UITableViewCell *cellOf;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.TableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    self.array = @[
        [[JackeZController alloc]initWithTitleName:@"personageCenter" viewClass:Base1View.class],
        [[JackeZController alloc]initWithTitleName:@"jijakjfkjsnfkanfnvknzmnfkeajfksjfknsakfjaksfsafasfksfn" viewClass:Base1View.class],
        [[JackeZController alloc]initWithTitleName:@"jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" viewClass:Base1View.class]];
    
    
    
    
    UINib *cellNib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
    [self.TableView registerNib:cellNib forCellReuseIdentifier:@"Re"];
    self.cellOf = [self.TableView dequeueReusableCellWithIdentifier:@"Re"];
    TableViewCell *cel = [[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil]firstObject];
    /**
     * 自适应前的尺寸
     */
    CGSize size = [cel.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    self.TableView.delegate = self;
    self.TableView.dataSource = self;
    NSLog(@"h=%f🐯,w=%f", size.height, size.width);
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return self.array.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    return 50;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TableViewCell *cell = [self.TableView dequeueReusableCellWithIdentifier:@"Re"];
    UIViewController *control = self.array[indexPath.row];
    
    cell.titleView.text = control.title;
    
    
    return cell;
    
}

//lable在tableViewCell的自适应高计算
//不能用TableViewCell *cell =   [tableView cellForRowAtIndexPath:indexPath];方法获取TableViewCell，不然会内存泄漏，自能用一个替换
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    TableViewCell *cell = (TableViewCell *)self.cellOf;
    cell.translatesAutoresizingMaskIntoConstraints = NO;
    cell.titleView.translatesAutoresizingMaskIntoConstraints = NO;
    cell.titleView.text = [[self.array objectAtIndex:indexPath.row] title];
    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    NSLog(@"titel=%@,h=%f,w=%f",cell.titleView.text, size.height, size.width);
    
    
    
    
    
    return size.height + 1;
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *controller = self.array[indexPath.row];
    
    [self presentViewController:controller animated:YES completion:^{
        
    }];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
